package com.naodong.shenluntiku.mvp.view.adapter;

import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.mvp.model.bean.Analysis;
import com.naodong.shenluntiku.mvp.model.bean.CollectDetail;
import com.naodong.shenluntiku.mvp.model.bean.SubjectInfo;
import com.naodong.shenluntiku.mvp.model.bean.SubjectOption;
import com.naodong.shenluntiku.util.ImageLoaderUtil;

import me.shingohu.man.util.UiUtils;

/**
 * Created by hujie on 17/5/22.
 */

public class CollectListAdapter extends BaseMultiItemQuickAdapter<CollectDetail, BaseViewHolder> {


    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     */
    public CollectListAdapter() {
        super(null);
        addItemType(CollectDetail.SUBJECT_ANALYSIS, R.layout.list_subjectanalysis_item);
        addItemType(CollectDetail.VIDEO_ANALYSIS, R.layout.list_videoanalysis_item);
        addItemType(CollectDetail.SUBJECT, R.layout.swipe_item_subject);
    }

    @Override
    protected void convert(BaseViewHolder helper, CollectDetail item) {


        switch (item.getItemType()) {
            case CollectDetail.SUBJECT_ANALYSIS:
            case CollectDetail.VIDEO_ANALYSIS:
                Analysis analysis = (Analysis) item.getCollectDetail();

                if (analysis != null) {
                    helper.setText(R.id.titleTV, analysis.getTitle())
                            .setText(R.id.timeTV, analysis.getTime());
                    if (analysis.getListImgLink() == null) {
                        analysis.setListImgLink("");
                    }
                    ImageLoaderUtil.loadImage(mContext, analysis.getListImgLink(), helper.getView(R.id.imageIV));
                }
                break;
            case CollectDetail.SUBJECT:

                SubjectInfo subjectInfo = (SubjectInfo) item.getCollectDetail();
                if (subjectInfo == null) {
                    return;
                }

                helper.setText(R.id.questionTV, Html.fromHtml(subjectInfo.getSubCon()));

                LinearLayout optionContainerView = helper.getView(R.id.optionContainer);
                optionContainerView.removeAllViews();

                for (SubjectOption option : subjectInfo.getSubOptions()) {
                    View optionView = mLayoutInflater.inflate(R.layout.collect_subject_item_option, null);
                    TextView optionIndexTV = (TextView) optionView.findViewById(R.id.optionIndexTV);
                    TextView optionTV = (TextView) optionView.findViewById(R.id.optionTV);
                    optionIndexTV.setText(option.getOpName());
                    optionTV.setText(option.getOpCon());
                    optionContainerView.addView(optionView);

                    if (option.getOpIsCorrect() == 1) {
                        optionTV.setTextColor(UiUtils.getColor(R.color.color72CEB7));
                        optionIndexTV.setText("");
                        optionIndexTV.setBackgroundResource(R.drawable.ico_circle_right);
                    } else {
                        if (subjectInfo.getuChooseIds().contains(option.getOpId())) {
                            optionIndexTV.setTextColor(UiUtils.getColor(R.color.colorAccent));
                            optionIndexTV.setBackgroundResource(R.drawable.ico_circle_error);
                            optionIndexTV.setText("");
                        }
                    }
                }


                break;
        }
    }


}
